/*
 * Created by JFormDesigner on Thu Nov 15 14:59:25 CST 2018
 */

package cn.xo68.ide.plugin.ui.form;

import cn.xo68.core.util.StringTools;
import cn.xo68.ide.plugin.common.GuiUtils;
import cn.xo68.ide.plugin.common.PluginPathUtils;
import cn.xo68.ide.plugin.common.component.StarupApplicationComponent;
import cn.xo68.ide.plugin.common.component.ToolSuiteJobManager;
import cn.xo68.ide.plugin.common.domain.ProjectSetting;
import cn.xo68.ide.plugin.common.jobs.CreateProjectJob;
import cn.xo68.ide.plugin.common.resources.Messages;
import cn.xo68.ide.plugin.common.service.Settings;
import cn.xo68.ide.plugin.common.template.TemplateConfig;
import cn.xo68.ide.plugin.common.template.TemplateItem;
import com.intellij.ide.actions.OpenProjectFileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.wy2.generator.config.GeneratorConsts;
import icons.WxIcons;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wyy
 */
public class CreateProjectDialog extends JDialog {

    private static Logger logger = LoggerFactory.getLogger(CreateProjectDialog.class);

    public static void open(Project selectedProject, VirtualFile selectedFile){

        //logger.info(PluginConstants.getPluginDescriptor().getName());

        CreateProjectDialog dialog=new CreateProjectDialog(selectedProject, selectedFile);
        dialog.setIconImage(WxIcons.loadImage(WxIcons.PATH_PROJECT));
        GuiUtils.openDialog(dialog, Messages.DIALOG_CREATE_PROJECT_TITLE,550,400);




    }
    private final Project selectedProject;
    private final VirtualFile selectedFile;

    public CreateProjectDialog(Project selectedProject, VirtualFile selectedFile) {
        super((Frame) GuiUtils.getIdeaWindow());
        this.selectedProject = selectedProject;
        this.selectedFile = selectedFile;

        initComponents();
        initData();
    }



    protected void initData(){

        okButton.setText(Messages.DIALOG_CREATE_PROJECT_GENERATE);
        cancelButton.setText(Messages.DIALOG_CLOSE_TEXT);

        StarupApplicationComponent starupApplicationComponent = StarupApplicationComponent.getInstance();
        TemplateConfig templateConfig = starupApplicationComponent.loadTemplateConfig();

        DefaultComboBoxModel<TemplateItem> comboBoxModel= new DefaultComboBoxModel<>(templateConfig.getProjectItems());
        cbProjectTemplate.setModel(comboBoxModel);



        logger.info("{},{}",selectedProject, selectedFile);

        // Project project = starupApplicationComponent.getProject();
        if(selectedFile!=null && selectedFile.isDirectory()){
            tfLocation.setText(selectedFile.getCanonicalPath());
        } else {
            tfLocation.setText(VfsUtil.getUserHomeDir().getCanonicalPath());
        }
        ProjectSetting projectSetting= Settings.getInstance().getProjectSetting();
        tfGroupId.setText(projectSetting.getDefaultGroupId());
        tfArtifactId.setText(projectSetting.getDefaultArtifactId());
        tfVersion.setText(projectSetting.getDefaultVersion());
        tfPackages.setText(projectSetting.getDefaultPackages());
    }

    private void cancelButtonActionPerformed(ActionEvent e) {
        dispose();
    }

    private void okButtonActionPerformed(ActionEvent e) {

        Map<String, Object> config= new HashMap<>();

        TemplateItem selectedItem= (TemplateItem) cbProjectTemplate.getSelectedItem();
        File templateDirectory=new File(PluginPathUtils.getTemplatePath(), selectedItem.getTemplateFolder());
        config.put(GeneratorConsts.GENERATOR_CONFIG_TEMPLATE_DIRECTORY, templateDirectory.getAbsolutePath());

        config.put(GeneratorConsts.GENERATOR_CONFIG_OUTPUT_DIRECTORY, tfLocation.getText());

        config.put(GeneratorConsts.TEMPLATE_ATTRI_PROJECT_NAME, tfArtifactId.getText());
        config.put(GeneratorConsts.TEMPLATE_ATTRI_GROUP_ID, tfGroupId.getText());
        config.put(GeneratorConsts.TEMPLATE_ATTRI_ARTIFACT_ID, tfArtifactId.getText());
        config.put(GeneratorConsts.TEMPLATE_ATTRI_BASEPACKAGE, tfPackages.getText());

        config.put(GeneratorConsts.TEMPLATE_PROJECT_SOURCE_DIRECTORY, "src/main/java");
        config.put("resourcesDirectory", "src/main/resources");
        config.put("version", tfVersion.getText());
        String projectName=tfArtifactId.getText().replace("-","_");
        projectName= StringTools.makeAllWordFirstLetterUpperCase(projectName);
        config.put("projectName", projectName);

        CreateProjectJob createProjectJob=new CreateProjectJob("创建项目:"+ tfArtifactId.getText(),config, selectedProject, selectedFile);
        ToolSuiteJobManager toolSuiteJobManager=this.selectedProject.getComponent(ToolSuiteJobManager.class);
        toolSuiteJobManager.submit(createProjectJob);

        dispose();
    }

    private void btnBrowerActionPerformed(ActionEvent e) {
        final FileChooserDescriptor descriptor = new OpenProjectFileChooserDescriptor(false);

        FileChooser.chooseFile(descriptor, this.selectedProject, this.selectedProject.getBaseDir(), chooseFile -> {
            tfLocation.setText(chooseFile.getCanonicalPath());
        });
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        label1 = new JLabel();
        cbProjectTemplate = new JComboBox();
        label2 = new JLabel();
        tfLocation = new JTextField();
        btnBrower = new JButton();
        label3 = new JLabel();
        label4 = new JLabel();
        tfGroupId = new JTextField();
        label5 = new JLabel();
        tfArtifactId = new JTextField();
        label6 = new JLabel();
        tfVersion = new JTextField();
        lbPackage = new JLabel();
        tfPackages = new JTextField();
        buttonBar = new JPanel();
        okButton = new JButton();
        cancelButton = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setLayout(new GridBagLayout());
                ((GridBagLayout)contentPanel.getLayout()).columnWidths = new int[] {75, 255, 60, 0};
                ((GridBagLayout)contentPanel.getLayout()).rowHeights = new int[] {35, 35, 35, 35, 35, 35, 30, 0};
                ((GridBagLayout)contentPanel.getLayout()).columnWeights = new double[] {0.0, 1.0, 0.0, 1.0E-4};
                ((GridBagLayout)contentPanel.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};

                //---- label1 ----
                label1.setText("\u9879\u76ee\u6a21\u677f");
                contentPanel.add(label1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
                    new Insets(0, 0, 5, 5), 0, 0));
                contentPanel.add(cbProjectTemplate, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 5, 5), 0, 0));

                //---- label2 ----
                label2.setText("\u4f4d\u7f6e");
                contentPanel.add(label2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                    GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
                    new Insets(0, 0, 5, 5), 0, 0));

                //---- tfLocation ----
                tfLocation.setEditable(false);
                contentPanel.add(tfLocation, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 5, 5), 0, 0));

                //---- btnBrower ----
                btnBrower.setText("\u6d4f\u89c8");
                btnBrower.addActionListener(e -> btnBrowerActionPerformed(e));
                contentPanel.add(btnBrower, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 5, 0), 0, 0));

                //---- label3 ----
                label3.setText("Maven\u4fe1\u606f");
                contentPanel.add(label3, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
                    GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
                    new Insets(0, 0, 5, 5), 0, 0));

                //---- label4 ----
                label4.setText("GroupId");
                contentPanel.add(label4, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,
                    GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
                    new Insets(0, 0, 5, 5), 0, 0));
                contentPanel.add(tfGroupId, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 5, 5), 0, 0));

                //---- label5 ----
                label5.setText("ArtifactId");
                contentPanel.add(label5, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0,
                    GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
                    new Insets(0, 0, 5, 5), 0, 0));
                contentPanel.add(tfArtifactId, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 5, 5), 0, 0));

                //---- label6 ----
                label6.setText("Version");
                contentPanel.add(label6, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0,
                    GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
                    new Insets(0, 0, 5, 5), 0, 0));
                contentPanel.add(tfVersion, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 5, 5), 0, 0));

                //---- lbPackage ----
                lbPackage.setText("Packages");
                contentPanel.add(lbPackage, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0,
                    GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
                    new Insets(0, 0, 0, 5), 0, 0));
                contentPanel.add(tfPackages, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 5), 0, 0));
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setLayout(new GridBagLayout());
                ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 80};
                ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0};

                //---- okButton ----
                okButton.setText("OK");
                okButton.addActionListener(e -> okButtonActionPerformed(e));
                buttonBar.add(okButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 5), 0, 0));

                //---- cancelButton ----
                cancelButton.setText("Cancel");
                cancelButton.addActionListener(e -> cancelButtonActionPerformed(e));
                buttonBar.add(cancelButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 0), 0, 0));
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JLabel label1;
    private JComboBox cbProjectTemplate;
    private JLabel label2;
    private JTextField tfLocation;
    private JButton btnBrower;
    private JLabel label3;
    private JLabel label4;
    private JTextField tfGroupId;
    private JLabel label5;
    private JTextField tfArtifactId;
    private JLabel label6;
    private JTextField tfVersion;
    private JLabel lbPackage;
    private JTextField tfPackages;
    private JPanel buttonBar;
    private JButton okButton;
    private JButton cancelButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
